using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;

//using WP.Commons;

namespace WP.Controls
{
    public partial class TimeEx : System.Windows.Forms.MaskedTextBox
    {
        string m_strIniTime = "00:00";
        bool m_bTimeNull=true;
        bool m_bSetOldTime=false;
        string m_strOldTime = "00:00";

        public TimeEx()
        {
            InitializeComponent();
        }

        public TimeEx(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
            this.Click += new EventHandler(TimeEx_Click);
            this.LostFocus += new EventHandler(TimeEx_LostFocus);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(TimeEx_KeyDown);
            this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(TimeEx_KeyPress);
            this.TextChanged += new EventHandler(TimeEx_TextChanged);
        }

        #region Private Events
        private void TimeEx_Click(object sender, EventArgs e)
        {
            if (!m_bTimeNull)
            {
                SelNext(0);
            }
        }

        private void TimeEx_LostFocus(object sender, EventArgs e)
        {
            string strCorrectTime;

            if (!m_bTimeNull)
            {
                strCorrectTime = ValidTime(this.Text);
                this.Text = strCorrectTime;
            }
        }

        private void TimeEx_TextChanged(object sender, EventArgs e)
        {
            if (m_bSetOldTime)
                this.Text = m_strOldTime;

            if (this.Text.Trim().Length < 1 || this.Text == "  :")
            {
                m_bTimeNull = true;
//                this.Text = GetNullValue();
            }
        }

        private void TimeEx_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            int intMidKeyAscii;
            intMidKeyAscii = Convert.ToInt32(e.KeyChar);

            if (intMidKeyAscii > 47 && intMidKeyAscii < 58)
            {
                if (this.SelectionStart >= 5)
                    return;
                else
                {
                    int intMidPos;
                    string strMid;
                    string strRight;
                    if (m_bTimeNull)
                    {
                        this.Text = (Convert.ToChar(intMidKeyAscii).ToString()) + m_strIniTime.Substring(1);
                        m_bTimeNull = false;
                        this.SelectionStart = 1;
                    }
                    else
                    {
                        //If this.SelectionStart = 2 Or this.SelectionStart = 5 Then
                        if (!ComFunc.IsNumeric(this.Text.Substring(this.SelectionStart, 1)))
                            intMidPos = this.SelectionStart + 1;
                        else
                            intMidPos = this.SelectionStart;

                        strMid = this.Text.Substring(0, intMidPos);
                        strRight = this.Text.Substring(intMidPos + 1);
                        this.Text = strMid + Convert.ToChar(intMidKeyAscii).ToString() + strRight;
                        this.SelectionStart = intMidPos + 1;
                    }
                }
            }
            e.KeyChar = Convert.ToChar(Keys.None);
        }

        private void TimeEx_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (this.SelectionLength == this.Text.Length)
                {
                    this.Text = "__:__";
                    m_strIniTime = "00:00";
                    this.SelectionStart = 0;
                    m_bSetOldTime = false;
                }
                else
                {
                    m_bSetOldTime = true;
                }
                //In MaskedTextBox control, e.SuppressKeyPress can not be valid.
                //e.SuppressKeyPress = true;
                //return;
            }
            else if (e.KeyCode == Keys.Back)
            {
                m_bSetOldTime = true;
                //e.SuppressKeyPress = true;
                //return;
            }
            else
            {
                m_bSetOldTime = false;
            }
            m_strOldTime = this.Text;
        }

        #endregion 

        #region Private Functions
        private string ValidTime(string strTime)
        {
            int intHour;
            int intMinute;
            string strHour="";
            string strMinute="";
            string strValidTime;

            try
            {
                intHour   = Convert.ToInt32(strTime.Substring(0,2));//GetTimePart(strTime, 1);
                intMinute = Convert.ToInt32(strTime.Substring(3, 2));//GetTimePart(strTime, 2);


                if (intHour > 23)
                {
                    intHour = Convert.ToInt32(m_strIniTime.Substring(0,2));
                }

                if (intMinute > 59)
                {
                    intMinute = Convert.ToInt32(m_strIniTime.Substring(3,2));
                }

                if (intHour.ToString().Length == 1)
                    strHour = "0" + intHour.ToString();
                else
                    strHour = intHour.ToString();

                if (intMinute.ToString().Length == 1)
                    strMinute = "0" + intMinute.ToString();
                else
                    strMinute = intMinute.ToString();

                strValidTime = strHour + ":" + strMinute;
                m_strIniTime = strValidTime;
            }
            catch
            {
                strValidTime = m_strIniTime;
            }

            return strValidTime;
        }

        private string GetNullValue()
        {
            int i;
            string strDate;

//            strDate = DateTime.Today.ToString("MM/dd/yyyy");
            strDate = m_strIniTime;

            for (i = 1; i <= strDate.Length; i++)
            {
                if (ComFunc.IsNumeric(strDate.Substring(i - 1, 1)))
                {
//                   strDate.Substring(i, 1) = " ";
                }
            }

            return strDate;
        }

        private void SelNext(int intNext)
        {
            int i;
            int intpos1;
            intpos1 = 0;  //position of the first non-numeric

            for (i = 1; i <= this.Text.Length; i++)
            {
                if (!ComFunc.IsNumeric(this.Text.Substring(i - 1, 1)))
                {
                    intpos1 = i;
                }
            }

            int intSeledLen;
            intSeledLen = this.SelectionLength;
            switch (intNext)
            {
                case -1:
                    if (this.SelectionStart - this.SelectionLength >= 0)
                    {
                        this.SelectionStart = this.SelectionStart - intSeledLen;
                        this.SelectionLength = intSeledLen;
                    }
                    else
                    {
                        intSeledLen = this.SelectionStart;
                        this.SelectionStart = 0;
                        this.SelectionLength = intSeledLen;
                    }
                    break;
                case 0:
                    if (this.SelectionStart < intpos1)
                    {
                        this.SelectionStart = 0;
                        this.SelectionLength = intpos1 - 1;
                    }
                    else if (this.SelectionStart < 6)
                    {
                        this.SelectionStart = intpos1;
                        this.SelectionLength = 6 - intpos1 - 1;
                    }
                    //    else
                    //    {
                    //        this.SelectionStart = intpos2;
                    //        this.SelectionLength = this.Text.Length - intpos2;
                    //    }
                    break;
                case 1:
                    if (this.SelectionStart + 2 * intSeledLen > this.Text.Length)
                    {
                        this.SelectionStart = this.SelectionStart + intSeledLen;
                        this.SelectionLength = this.Text.Length - this.SelectionStart;
                    }
                    else
                    {
                        this.SelectionStart = this.SelectionStart + intSeledLen;
                        this.SelectionLength = intSeledLen;
                    }
                    break;
            }
        }

        #endregion

    }
}
